package com.sqfw.project.learnRecord.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sqfw.common.utils.SecurityUtils;
import com.sqfw.project.learnRecord.domain.LearnRecordComment;
import com.sqfw.project.learnRecord.mapper.LearnRecordCommentMapper;
import com.sqfw.project.learnRecord.mapper.LearnRecordListenMapper;
import com.sqfw.project.learnRecord.service.ILearnRecordCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author BlueSony
 */
@Service
public class LearnRecordCommentServiceImpl extends ServiceImpl<LearnRecordCommentMapper, LearnRecordComment> implements ILearnRecordCommentService {

    @Autowired
    private LearnRecordCommentMapper learnRecordCommentMapper;

    @Autowired
    private LearnRecordListenMapper learnRecordListenMapper;

    @Override
    public int insertLearnRecordComment(LearnRecordComment learnRecordComment) {
        learnRecordComment.setCreateBy(Optional.ofNullable(SecurityUtils.getLoginUser()).map(loginUser -> loginUser.getUsername()).orElse(""));
        return learnRecordCommentMapper.insert(learnRecordComment);
    }

    @Override
    public List<LearnRecordComment> selectLearnRecordCommentByRecordId(Long recordId) {
//        LambdaQueryWrapper<LearnRecordComment> commentQuery = new LambdaQueryWrapper<>();
//        commentQuery.eq(LearnRecordComment::getRecordId, recordId);
//        List<LearnRecordComment> allRecordComments = learnRecordCommentMapper.selectList(commentQuery);
        List<LearnRecordComment> allRecordComments = learnRecordCommentMapper.queryList(recordId);
        Map<Long, List<LearnRecordComment>> commentGroupByParentId = allRecordComments.stream().collect(Collectors.groupingBy(comment ->
                        comment.getParentId() != null ? comment.getParentId() : -1L, // 将 null 转为 -1L
                HashMap::new,
                Collectors.toList()));
        List<LearnRecordComment> rootComments = commentGroupByParentId.get(-1L);
        if (rootComments != null) {
            for (LearnRecordComment rootComment : rootComments) {
                rootComment.setReplies(getReplies(rootComment.getId(), commentGroupByParentId));
            }
        }
        return rootComments;
    }

    @Override
    public boolean canComment(LearnRecordComment learnRecordComment) {
        Integer totalSeconds = learnRecordListenMapper.queryTotalSeconds(learnRecordComment);
        if(totalSeconds >= learnRecordComment.getRecordDuration()){
            return true;
        }
        return false;
    }

    private List<LearnRecordComment> getReplies(Long parentId, Map<Long, List<LearnRecordComment>> commentGroupByParentId) {
        List<LearnRecordComment> replies = commentGroupByParentId.get(parentId);
        if (replies != null) {
            for (LearnRecordComment reply : replies) {
                reply.setReplies(getReplies(reply.getId(), commentGroupByParentId));
            }
        }
        return replies != null ? replies : new ArrayList<>();
    }
}




